<template>
  <div class="departments-container">
    <div class="app-container">
      <!-- 卡片组件(分为头部和身体) -->
      <el-card class="box-card">
        <!--       
<el-tabs v-model="activeName" @tab-click="handleClick">
    <el-tab-pane label="用户管理" name="first">用户管理</el-tab-pane>
  </el-tabs> -->
        <!-- 头部 -->
        <div slot="header" class="clearfix">
          <!-- tab导航 -->
          <el-tabs v-model="activeName" @tab-click="handleClick">
            <el-tab-pane label="组织导航" name="first" class="tab-pane">
              <el-row
                type="flex"
                justify="space-between"
                align="middle"
                class="department-header"
              >
                <!-- 第一列20份 -->
                <el-col :span="20">
                  <i class="el-icon-s-home" />
                  <span class="company">江苏传智播客教育科技股份有限公司</span>
                </el-col>
                <!-- 第二列4份 -->
                <el-col :span="4">
                  <!-- 在容器范围里, 一行再分2列 -->
                  <el-row type="flex" justify="end">
                    <el-col>负责人</el-col>
                    <el-col>
                      <!-- 这个之前在头部导航右侧下拉菜单讲过 -->
                      <el-dropdown>
                        <!-- 下拉菜单文字 -->
                        <span class="el-dropdown-link">
                          操作<i class="el-icon-arrow-down el-icon--right" />
                        </span>
                        <!-- 下拉项 -->
                        <el-dropdown-menu slot="dropdown">
                          <el-dropdown-item @click.native="add()"
                            >添加子部门</el-dropdown-item
                          >
                        </el-dropdown-menu>
                      </el-dropdown>
                    </el-col>
                  </el-row>
                </el-col>
              </el-row>
              <!-- 列表 -->
              <el-tree
                class="departments-tree"
                :data="treeData"
                :props="defaultProps"
                default-expand-all
                node-key="id"
                :expand-on-click-node="false"
                @node-click="handleNodeClick"
              >
                <template #default="{ data }">
                  <el-row
                    type="flex"
                    justify="space-between"
                    align="middle"
                    style="height: 40px; width: 100%"
                  >
                    <!-- 部门名字 -->
                    <el-col :span="20">
                      <span>{{ data.name }}</span>
                    </el-col>
                    <!-- 功能操作区域 -->
                    <el-col :span="4">
                      <el-row type="flex" justify="end">
                        <!-- 两个内容 -->
                        <el-col>{{ data.manager }}</el-col>
                        <el-col>
                          <!-- 下拉菜单 element -->
                          <el-dropdown>
                            <span> 操作<i class="el-icon-arrow-down" /> </span>
                            <!-- 下拉菜单 -->
                            <el-dropdown-menu slot="dropdown">
                              <el-dropdown-item @click.native="add(data)"
                                >添加子部门</el-dropdown-item
                              >
                              <el-dropdown-item @click.native="edit(data)"
                                >编辑部门</el-dropdown-item
                              >
                              <el-dropdown-item
                                @click.native="del(data)"
                                v-if="!data.children"
                                >删除部门</el-dropdown-item
                              >
                            </el-dropdown-menu>
                          </el-dropdown>
                        </el-col>
                      </el-row>
                    </el-col>
                  </el-row>
                </template>
              </el-tree>
            </el-tab-pane>
          </el-tabs>
        </div>
      </el-card>

      <!-- 弹出框 -->
      <!-- <DepartDialog /> -->
      <departDialog
        :dialogVisiblefn="dialogVisiblefn"
        @upcancel="cancel"
        @addupcertain="certain"
        :employess="employess"
        ref="departDialogfn"
        :originlist='originlist'
        :is-edit='isEdit'
        :clickpid='clickpid'
      ></departDialog>
    </div>
  </div>
</template>

<script>
import {
  getdepartmentAPI,
  getdepartment,
  getEmployessSimpleListAPI,
  createdDepartmentAPI,
  updateDepartmentsAPI,
  delDepartmentAPI,
} from "../../api/department";
import { transTree } from "../../utils/index";
import departDialog from "./components/departDialog.vue";

export default {
  name: "Departments",
  data() {
    return {
      //弹出框属性
      dialogVisiblefn: false,
      activeName: "first",
      treeData: [
        {
          departName: "总裁办",
          name: "孙财",
        },
        {
          departName: "行政部",
          name: "罗晓晓",
        },
        {
          departName: "人事部",
          name: "安小晴",
          children: [
            {
              departName: "财务核算部",
            },
          ],
        },
        {
          departName: "财务部",
          name: "",
          children: [
            {
              departName: "财务核算部",
            },
            {
              departName: "税务管理部",
            },
            {
              departName: "薪资管理部",
            },
          ],
        },
        {
          departName: "技术部",
          name: "",
          children: [
            {
              departName: "前端技术部",
            },
            {
              departName: "后端技术部",
            },
          ],
        },
        {
          departName: "运营部",
          name: "",
        },
        {
          departName: "市场部",
          name: "武高丽",
          children: [
            {
              departName: "市场部1",
            },
            {
              departName: "市场部2",
            },
          ],
        },
      ],
      // 定义结构显示
      defaultProps: {
        children: "children",
        label: "departName",
      },
      //员工简单列表
      employess: [],
      //data里面的pid
      clickpid: "",
      //添加编辑状态
      isEdit: false,
      originlist:[] //用于校验的部门列表数组
    };
  },
  components: {
    // DepartDialog
    departDialog,
  },
  created() {
    //为了重复调用，所以封装请求
    this.getdepartmentFn();
    this.getEmployessSimpleListAPIFN();
  },
  methods: {
    //获取部门组织架构
    async getdepartmentFn() {
      const res = await getdepartment();
      this.originlist=res.data.data.depts.map(item=>({
         id:item.id,
         code:item.code,
         name:item.name,
         pid:item.pid
      })),
      // console.log(this.originlist);
      this.treeData = transTree(res.data.data.depts, "");
    },
    //获取部门 员工简单信息
    async getEmployessSimpleListAPIFN() {
      const res = await getEmployessSimpleListAPI();
      this.employess = res.data.data;
      // console.log(employess);
    },

    handleNodeClick() {},
    handleClick() {
      // console.log(1);
    },

    // 正文部分-右侧的添加子部门
    add(data) {
      if (data == undefined) {
        this.clickpid = "";
      } else {
        this.clickpid = data.id;
      }
      (this.isEdit = false),
      (this.dialogVisiblefn = true);
      
    },
    // 编辑子部分
    async edit(data) {
      this.isEdit = true;
      this.dialogVisiblefn = true;
      const res = await getdepartmentAPI(data.id);
      this.$refs.departDialogfn.form = res.data.data;
      // console.log(res);
    },
    // 删除部分
    del(dataw) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delDepartmentAPI(dataw.id).then((res) => {
            // console.log(res);
            this.$message({
              type: "success",
              message: "删除成功!",
            });

            this.getdepartmentFn();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    //子组件点击取消按钮
    cancel() {
      this.dialogVisiblefn = false;
    },
    //子组件点击确定按钮
    certain(fromobj) {
      if (this.isEdit) {
        // 编辑
        updateDepartmentsAPI(fromobj).then((res) => {
          // console.log(res);
        });
        this.getdepartmentFn();
        this.dialogVisiblefn = false;
      } else {
        //添加

        this.dialogVisiblefn = false;
        fromobj.pid = this.clickpid;
        createdDepartmentAPI(fromobj).then((res) => {
          // console.log(res);
          //添加完成以后刷新
          this.getdepartmentFn();
        });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.el-tree-node__content {
  height: 30px !important;
  font-size: 14px !important;
}
.departments-tree {
  margin: 12px 0px;
}
.box-card {
  padding: 10px 30px;
}

.tab-pane {
  padding: 20px 80px;

  .company {
    margin-left: 12px;
    font-weight: bold;
    font-size: 14px;
  }

  .departments-container {
    height: 50px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
  }
}
</style>